package com.acce.base.utils;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

public class PropertiesUtil
{
  private static Properties properties = new Properties();

  public static PropertiesUtil getInstance()
  {
    return Holder.instance;
  }

  public Properties getProperties(String cfgFilePath)
  {
    try
    {
      properties = new Properties();
      properties.load(new FileInputStream(getPropertiesPath(cfgFilePath)));
    }
    catch (FileNotFoundException e) {
      properties = null;
      e.printStackTrace();
    }
    catch (IOException e) {
      properties = null;
      e.printStackTrace();
    }
    return properties;
  }

  public String getPropertiesPath(String cfgFilePath)
  {
    String resourcePath = PropertiesUtil.class.getResource("/").getPath();
//    String webInfPath = resourcePath.substring(0, resourcePath.lastIndexOf("/classes")).toString();
    cfgFilePath = StringUtils.trimToEmpty(cfgFilePath);
    if (StringUtils.isNotBlank(cfgFilePath)) {
      return resourcePath.concat(cfgFilePath).toString();
    }

    return resourcePath;
  }

  public static String getConfigurationPath(String cfgFilePath)
  {
    String resourcePath = PropertiesUtil.class.getResource("/").getPath();
//    String webInfPath = resourcePath.substring(0, resourcePath.lastIndexOf("/classes")).toString();
    cfgFilePath = StringUtils.trimToEmpty(cfgFilePath);
    if (StringUtils.isNotBlank(cfgFilePath)) {
      return resourcePath.concat(cfgFilePath).toString();
    }

    return resourcePath;
  }

  public static void main(String[] args)
    throws Exception
  {
//	  Map<String, String> ckformparams = new HashMap<String, String>();
//		ckformparams.put("touser", "oFH1xwfGPxCQb8UHkUC2KoUjw6zc");
//		ckformparams.put("msgtype", "txt");
//		ckformparams.put("text", "{\"content\":\"Hello World\"}");
//		String serviceString = sendhttp("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=ACCESS_TOKEN", ckformparams);
	  
	  Map<String, String> ckformparams = new HashMap<String, String>();
		ckformparams.put("touser", "oFH1xwfGPxCQb8UHkUC2KoUjw6zc");
		ckformparams.put("msgtype", "txt");
		ckformparams.put("text", "{\"content\":\"Hello World\"}");
//		String serviceString = sendhttp("https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=ACCESS_TOKEN", ckformparams);
	  
	  
//    PropertiesUtil propertiesUtil = getInstance();
//    if (propertiesUtil == null) {
//      System.out.println("pu is null");
//    }
//    else {
//      System.out.println(propertiesUtil);
//    }
//    PropertiesUtil propertiesUtil2 = getInstance();
//    System.out.println(propertiesUtil2);
  }
  
  public static String sendhttp(String serviceAddr, Map<String, String> params){
		String returnString = "";
		// 创建默认的httpClient实例.    
      CloseableHttpClient httpclient = HttpClients.createDefault();  
      try{
      	// 创建httppost  
	        HttpPost httppost = new HttpPost(serviceAddr);
	        List<NameValuePair> formparams = new ArrayList<NameValuePair>();  
	        Iterator<String> it = params.keySet().iterator();
	        while(it.hasNext()){
	        	String key = it.next();
	        	String value = params.get(key);
	        	formparams.add(new BasicNameValuePair(key,value));
	        }
	        UrlEncodedFormEntity uefEntitygs = new UrlEncodedFormEntity(formparams,"UTF-8");
	        httppost.setEntity(uefEntitygs); 
	        CloseableHttpResponse response = httpclient.execute(httppost);  //发送post请求并响应的内容
			try {
				HttpEntity entity = response.getEntity(); 
	            returnString=EntityUtils.toString(entity, "UTF-8");
			} catch (Exception e) {
				e.printStackTrace();
			}finally{
				response.close();
			}
      }catch(Exception e){
      	e.printStackTrace();
      }finally{
      	 // 关闭连接,释放资源    
          try {  
              httpclient.close();  
          } catch (IOException e) {  
              e.printStackTrace();  
          }  
      }
      return returnString;
	}

  private static class Holder
  {
    public static final PropertiesUtil instance = new PropertiesUtil();
  }
}
